#ifndef ROBOTMOTORI_H
#define ROBOTMOTORI_H

#include "RobotDef.h"
#include <QtCore>
#include <QtSerialPort>
#include <IceUtil/IceUtil.h>


#define MAX_CHAOSHENG 8
#define MAX_HONGWAI 8
#define MAX_REMIN 2
#define MAX_TOUCH 4
#define MAX_MOTOR 2
#define MAX_HIT 4

class TmSerialThread : public QThread
{
    Q_OBJECT
public:
    TmSerialThread();

    // 头部电机
    void HeadEnable(bool bLR, bool bUD);
    void HeadLrInit();
    void HeadUdInit();
    void HeadLrAngle(int nAngle, int nSpeed);
    void HeadUdAngle(int nAngle, int nSpeed);
    // 左手电机
    void HandLEnable(bool bSwing, bool bExpand);
    void HandLSwingInit();
    void HandLExpandInit();
    void HandLSwingAngle(int nAngle, int nSpeed);
    void HandLExpandAngle(int nAngle, int nSpeed);
    // 右手电机
    void HandREnable(bool bSwing, bool bExpand);
    void HandRSwingInit();
    void HandRExpandInit();
    void HandRSwingAngle(int nAngle, int nSpeed);
    void HandRExpandAngle(int nAngle, int nSpeed);
    // 轮子
    void WheelEnable(bool bEnable);
    void WheelStop(bool bSlow);
    void WheelRotate(int nAngle, int nSpeed);
    void WheelRun(int nDistance, int nSpeed);
    void WheelRunDiff(bool bLeftUP, int nLeftSpeed, bool bRight, int nRightSpeed);

    //LED
    void HeadEye(int nIndexF, int nIndexB);
    void HeadEye2(int nDelay, bool bR, bool bG, bool bB);
    void HeadEar(int nDelay, bool bR, bool bG, bool bB);
    void BodyLed(int nDelay, bool bR, bool bG, bool bB);
    void TouchSet(bool bFlash, int nColor, int nFPS);

    // Sensor
    void SensorBatteryValue(int nVal[2]);
    void SensorSonicValue(int nVal[MAX_CHAOSHENG]);
    void SensorInfraredValue(int nVal[MAX_HONGWAI]);

    // 避障功能
    void SetProtect(bool bOpen);
    bool GetProtect();

    void setExit();
    QByteArray make_sensor_json();

    void msg_push(QByteArray bCmd);
    bool msg_pop(QByteArray& bCmd);

    bool m_bIsMoving;
protected:
    void run();

private slots:
    void slot_new_data();
    void slot_new_data_extra();
    void slot_auto_send();
private:
    bool m_bExit;
    int _head_ud[3],_head_lr[3],_hand_left_e[3],_hand_left_s[3],_hand_right_e[3],_hand_right_s[3]; // 电机的限制角度
    int _v3_head_ud[3], _v3_head_lr[3];

    int _protect_distance[2]; // 超声&红外

    QTimer* m_pTmAutoSend;
    // varivable
    int pr_nChaosheng[MAX_CHAOSHENG]; // 超声波距离
    int pr_nHongwai[MAX_HONGWAI]; // 红外传感器距离
    bool pr_bRemin[MAX_REMIN]; // 热敏传感器
    bool pr_bTouch[MAX_TOUCH]; // 触摸事件
    bool pr_bHit[MAX_HIT]; // 碰撞事件
    bool pr_bMotorOver[MAX_MOTOR]; // 电机事件
    int pr_nMotorDisk[4]; // 电机码盘
    int pr_nBattery[2]; // 电池的电量/温度

    // serial
    int _parseOneCmd(QByteArray& inData, QByteArray& outData);
    QByteArray m_serialBuff;
    QSerialPort* m_pSerialPort;
    QByteArray m_extraSerialBuff;
    bool m_bOpenBoxSP;  //是否启用外置传感器盒(超声波、热释电)
    QSerialPort* m_pSerialPort_Box;  //外置传感器盒(超声波、热释电)
    IceUtil::Mutex m_MutexMsg;
    QList<QByteArray> m_ListMsg;

    // protect
    bool m_bProtectOpen;

};

class RobotMotorI : public ::Robot::Motor
{
public:
    RobotMotorI();
protected:
    // Motor interface
public:
    void SerialWrite(const Ice::ByteSeq &bData, const Ice::Current &);

    // 头部电机
    void HeadEnable(bool bLR, bool bUD, const Ice::Current &);
    void HeadLrInit( const Ice::Current &);
    void HeadUdInit( const Ice::Current &);
    void HeadLrAngle(Ice::Int nAngle, Ice::Int nSpeed, const Ice::Current &);
    void HeadUdAngle(Ice::Int nAngle, Ice::Int nSpeed, const Ice::Current &);
    // 左手电机
    void HandLEnable(bool bSwing, bool bExpand, const Ice::Current &);
    void HandLSwingInit( const Ice::Current &);
    void HandLExpandInit( const Ice::Current &);
    void HandLSwingAngle(Ice::Int nAngle, Ice::Int nSpeed, const Ice::Current &);
    void HandLExpandAngle(Ice::Int nAngle, Ice::Int nSpeed, const Ice::Current &);
    // 右手电机
    void HandREnable(bool bSwing, bool bExpand, const Ice::Current &);
    void HandRSwingInit( const Ice::Current &);
    void HandRExpandInit( const Ice::Current &);
    void HandRSwingAngle(Ice::Int nAngle, Ice::Int nSpeed, const Ice::Current &);
    void HandRExpandAngle(Ice::Int nAngle, Ice::Int nSpeed, const Ice::Current &);
    // 轮子
    void WheelEnable(bool bEnable, const Ice::Current &);
    void WheelStop(bool bSlow, const Ice::Current &);
    void WheelRotate(Ice::Int nAngle, Ice::Int nSpeed, const Ice::Current &);
    void WheelRun(Ice::Int nDistance, Ice::Int nSpeed, const Ice::Current &);
    void WheelRunDiff(bool bLeftUP, Ice::Int nLeftSpeed, bool bRight, Ice::Int nRightSpeed, const Ice::Current &);

    //LED
    void HeadEye(Ice::Int nIndexF, Ice::Int nIndexB, const Ice::Current &);
    void HeadEye2(Ice::Int nDelay, bool bR, bool bG, bool bB, const Ice::Current &);
    void HeadEar(Ice::Int nDelay, bool bR, bool bG, bool bB, const Ice::Current &);
    void BodyLed(Ice::Int nDelay, bool bR, bool bG, bool bB, const Ice::Current &);
    void TouchSet(bool bFlash, Ice::Int nColor, Ice::Int nFPS, const Ice::Current &);

    // Sensor
    // 电池信息
    void SensorBatteryValue(Ice::IntSeq &nValList, const Ice::Current &);
    // 声波传感器
    void SensorSonicValue(Ice::IntSeq &nValList, const Ice::Current &);
    // 红外传感器
    void SensorInfraredValue(Ice::IntSeq &nValList, const Ice::Current &);

    // 避障功能
    void SetProtect(bool bOpen, const Ice::Current &);
    bool GetProtect(const Ice::Current &);
};
#endif // ROBOTMOTORI_H
